package com.scy.coolshark.controller;

import com.scy.coolshark.mapper.ProductMapper;
import com.scy.coolshark.pojo.dto.ProductDTO;
import com.scy.coolshark.pojo.dto.ProductUpdateDTO;
import com.scy.coolshark.pojo.entity.Product;
import com.scy.coolshark.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product/")
public class ProductController {
    //@Value()给变量赋值      ${xxx}写法会去application.properties里面找到对应的配置信息
    @Value("${imagesPath}")
    private String dirPath;
    @Autowired(required = false)
    ProductMapper mapper;

    @RequestMapping("insert")
    public void insert(@RequestBody ProductDTO productDTO){
        System.out.println("productDTO = " + productDTO);
        Product p = new Product();
        BeanUtils.copyProperties(productDTO,p);
        p.setCreated(new Date());
        mapper.insert(p);
    }
    @RequestMapping("selectAdmin")
    public List<ProductAdminVO> selectAdmin(){
        return mapper.selectAdmin();
    }
    @RequestMapping("deleteById")
    public void deleteById(int id){
        //通过id查询图片路径并删除
        String url = mapper.selectUrlById(id);
        new File(dirPath+url).delete();
        //删除数据库数据
        mapper.deleteById(id);
    }
    @RequestMapping("selectUpdateById")
    public ProductUpdateVO selectUpdateById(int id){
        return mapper.selectUpdateById(id);
    }
    @RequestMapping("update")
    public void update(@RequestBody ProductUpdateDTO productUpdateDTO){
        System.out.println("proDuctUpdateDTO = " + productUpdateDTO);
        String url = mapper.selectUrlById(productUpdateDTO.getId());
        if (!url.equals(productUpdateDTO.getUrl())){
            new File(dirPath+url).delete();
        }
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDTO,product);
        mapper.update(product);
    }
    @RequestMapping("selectTop")
    public List<ProductTopVO> selectTop(){
        return mapper.selectTop();
    }
    @RequestMapping("selectIndex")
    public List<ProductIndexVO> selectIndex(){
        return mapper.selectIndex();
    }
//    @RequestMapping("selectDetail")
//    public List<Product>
    @RequestMapping("selectDetailById")
    public ProductDetailVO selectDetailById(int id, HttpSession session){
        //第一次从session里面通过id获取是获取不到内容的
        String info = (String) session.getAttribute("view" + id);
        if (info ==null){//info为null代表之前没有获取到值
            //让商品的浏览量加1
            mapper.updateViewCountById(id);
            //吧id存到session里面去
            session.setAttribute("view" + id,"visited");
        }

        return mapper.selectDetailById(id);
    }
    @RequestMapping("selectByCid")
    public List<ProductIndexVO> selectByCid(int id){
        return mapper.selectByCid(id);
    }
    @RequestMapping("selectByWd")
    public List<ProductIndexVO> selectByWd(String wd){
        return mapper.selectByWd(wd);
    }
}
